package taladro.vista.proyecto;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import taladro.instrucciones.InstMover;
import taladro.instrucciones.Instruccion;
import taladro.proyecto.Proyecto;

public class PanelPreview extends JPanel
{

    private Proyecto proyecto;
    public double zoomFactor;
    private JButton jButton1;
    private JComboBox jComboZoom;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JViewport jViewport;
    private ProyectoPainter panelProyecto;
    
    private Rule columnView;
    private Rule rowView;

    public PanelPreview(Proyecto proyecto)
    {
        this.proyecto = proyecto;
        initComponents();
        zoomFactor = 1D;
    }

    private void initComponents()
    {
        jLabel1 = new JLabel();
        jButton1 = new JButton();
        jPanel1 = new JPanel();
        jLabel2 = new JLabel();
        jComboZoom = new JComboBox();
        setLayout(new GridBagLayout());
        jLabel1.setText("Vista Previa");
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = 17;
        add(jLabel1, gridBagConstraints);
        //jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = 1;
        gridBagConstraints.weightx = 1.0D;
        gridBagConstraints.weighty = 1.0D;
        
        //jViewport.setView(panelProyecto);
        //jScrollPane1.setViewport(jViewport);
        panelProyecto = new ProyectoPainter(proyecto);
        jScrollPane1 = new JScrollPane(panelProyecto);
        
        //jScrollPane1.setHorizontalScrollBarPolicy(32);
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setPreferredSize(new Dimension(200, 200));
        
        
        //Create the row and column headers.
        columnView = new Rule(Rule.HORIZONTAL, true);
        rowView = new Rule(Rule.VERTICAL, true);
        
        columnView.setPreferredWidth(420);
        rowView.setPreferredHeight(480);
        
        jScrollPane1.setColumnHeaderView(columnView);
        jScrollPane1.setRowHeaderView(rowView);
        
        /*jScrollPane1.addHierarchyBoundsListener(new HierarchyBoundsListener() {
            public void ancestorMoved(HierarchyEvent hierarchyevent)
            {
            }
            public void ancestorResized(HierarchyEvent evt)
            {
                jScrollPane1AncestorResized(evt);
            }
        });*/
        
        
        
        add(jScrollPane1, gridBagConstraints);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }            
        });
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = 15;
        //add(jButton1, gridBagConstraints);
        jLabel2.setText("Zoom:");
        jPanel1.add(jLabel2);
        jComboZoom.setEditable(true);
        jComboZoom.setModel(new DefaultComboBoxModel(new String[] {
            "100", "120", "150", "200","300","400"
        }));
        jComboZoom.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent evt)
            {
                jComboZoomItemStateChanged(evt);
            }
        });
        jPanel1.add(jComboZoom);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        add(jPanel1, gridBagConstraints);
        System.out.println("Iniciados los componentes de panelPreview");
    }

    private void jComboZoomItemStateChanged(ItemEvent evt)
    {
        System.out.println("Cambio de Zoom " + evt.getItem());
        cambioZoom( evt.getItem().toString() );
    }
    
    public void refreshZoom(){
        cambioZoom( jComboZoom.getSelectedItem().toString() );
    }
    
    private void cambioZoom(String strZoom){
        try
        {
            double zoom = Double.parseDouble(strZoom);
            if(zoomFactor != zoom / 100D)
            {
                zoomFactor = zoom / 100D;
                panelProyecto.setZoomFactor(zoomFactor);
                columnView.setZoomFactor(zoomFactor);
                Dimension dim = panelProyecto.getPreferredSize();
                columnView.setPreferredWidth( (int) dim.getWidth() );
                rowView.setZoomFactor(zoomFactor);
                rowView.setPreferredHeight( (int) dim.getHeight() );
                jScrollPane1.revalidate();
                repaint();
                revalidate();
                //jScrollPane1.setPreferredSize(new Dimension(200, 200));
                //jScrollPane1.getViewport().setPreferredSize( panelProyecto.getPreferredSize()  );
                
                //redrawProject();
            }
        }
        catch(Exception e) { }
    }

    private void jButton1ActionPerformed(ActionEvent evt)
    {
        //redrawProject();
    }

    private void jScrollPane1AncestorResized(HierarchyEvent evt)
    {
        //redrawProject();
    }
    
//    public void paint(Graphics g) {
//        //redrawProject(g);
//    }
//
//    public void paintComponent(Graphics g)
//    {
//        //super.paintComponent(g);
//        //redrawProject();
//    }

//    public void redrawProject(Graphics g)
//    {
//        Vector vector = proyecto.getAdminInstrucciones().getVectorInstrucciones();
//        Iterator iterator = vector.listIterator();
//        double posX = 0.0D;
//        double posY = 0.0D;
//        Dimension dimTablilla = new Dimension();
//        proyecto.getDimProyecto(dimTablilla);
//        dimTablilla.setSize(dimTablilla.getWidth() * zoomFactor, dimTablilla.getHeight() * zoomFactor);
//        jScrollPane1.setSize(dimTablilla);
//        //Graphics g = jScrollPane1.getGraphics();
//        Graphics2D g2d = (Graphics2D) g;//jScrollPane1.getGraphics();
//        if(g != null)
//        {
//            do
//            {
//                if(!iterator.hasNext())
//                {
//                    break;
//                }
//                Instruccion instruccion = (Instruccion)iterator.next();
//                if(instruccion instanceof InstMover)
//                {
//                    InstMover instMover = (InstMover)instruccion;
//                    if(instMover.getDown() == 1)
//                    {
//                        g2d.draw( new Line2D.Double((int)Math.round(posX * zoomFactor), (int)Math.round(posY * zoomFactor), (int)Math.round(instMover.getPosX() * zoomFactor), (int)Math.round(instMover.getPosY() * zoomFactor) ));
//                        //g.drawLine((int)Math.round(posX * zoomFactor), (int)Math.round(posY * zoomFactor), (int)Math.round(instMover.getPosX() * zoomFactor), (int)Math.round(instMover.getPosY() * zoomFactor));
//                    }
//                    posX = instMover.getPosX();
//                    posY = instMover.getPosY();
//                }
//            } while(true);
//        }
//    }

}
